<?php

/**
 * 控制器基类.
 * 
 * @author aic <41262633@qq.com>
 */

namespace Controller;

/**
 * Class Base Controller.
 */
class Base {

    protected $data = []; // 模板变量存储.
    protected $param = [];
    protected $userInfo = []; // 账号信息
    protected $role = '';
    protected $referer = '';
    protected $wx_user = []; // 微信账号信息
    protected $openid = ''; // wx openid

    /**
     * 构造函数.
     * 
     * @param mixed $vals 附加的变量.
     */

    public function __construct($vals = array()) {
        $this->param = $vals;
        $this->assign('type', 'my');
        $this->getReferer();
        // 获取用户信息
        $this->getUserInfo();
        // 获取微信账号信息
        $this->getWxUser();
    }

    /**
     * 模板赋值.
     * 
     * @param string $key 赋值的变量名.
     * @param mixed $data 赋值.
     * 
     * @return void
     */
    protected function assign($key, $data) {
        $this->data[$key] = $data;
    }

    /**
     * 视图渲染.
     * 
     * @param string $tmpl 模板名.
     * @param boolean $get_content 是否输出内容.
     * 
     * @return void
     */
    protected function display($tmpl, $get_content = false) {
        $tpl_file = TPL_PATH . $tmpl . '.html'; // 模板文件地址.

        if (!file_exists($tpl_file)) {
            exit('模板文件' . $tmpl . '不存在');
        }

        if (!empty($this->data)) {
            // 从数组中将变量导入到当前的符号表.
            extract($this->data);
        }

        $out = '';

        // 打开输出控制缓冲.
        ob_start();
        require $tpl_file;

        if ($get_content) {
            $out = ob_get_contents();
            ob_end_clean();
            return $out;
        }

        // 冲刷出（送出）输出缓冲区内容并关闭缓冲.
        ob_end_flush();
        // display输出后强制终止后续代码.
        return $out;
    }

    /**
     * 获取表单提交的数据(post).
     * 
     * @param string $key 表单名.
     * 
     * @return mixed.
     */
    protected function getPost($key = '') {
        $value = isset($_POST[$key]) ? $_POST[$key] : '';
        return \Common::filterData($value);
    }

    /**
     * 获取表单提交的数据(get).
     * 
     * @param string $key 表单名.
     * 
     * @return mixed.
     */
    protected function getGet($key = '') {
        $value = isset($_GET[$key]) ? trim($_GET[$key]) : '';
        return \Common::filterData($value);
    }

    /**
     * 查询管理员登录信息
     */
    protected function getUserInfo() {
        $ret = \Common::getCookie(hash('md5', 'userInfo'), false);
        if (!empty($ret)) {
            $this->userInfo = \Common::inflate_data($ret);
        }
        $this->role = isset($this->userInfo['role']) ? $this->userInfo['role'] : '';
        $this->assign('userInfo', $this->userInfo);
        $this->assign('role', $this->role);
    }

    /**
     * 查询微信账号信息
     */
    protected function getWxUser() {
        $ret = \Common::getCookie('wx_user');
        $wx_user = [];

        if ($ret) {
            $ret = stripslashes($ret);
            $wx_user = json_decode($ret, true);
        }

        $openid = $this->openid = \Common::getCookie('openid');

        if ($openid && (!$wx_user || !isset($wx_user['id']) || !isset($wx_user['nickname']) || !isset($wx_user['role']) || !isset($wx_user['inviter']))) {
            $row = \Db::instance()->getRow("select * from `wx_user` where `openid`='$openid'");
            if ($row) {
                // cookie有效期一天
                \Common::setCookie('wx_user', json_encode($row), 86400);
                $wx_user = $row;
            }
        }

        $this->wx_user = $wx_user;

        $this->assign('wx_user', $wx_user);
        $this->assign('openid', $openid);
    }

    /**
     * 获取http_referer
     */
    protected function getReferer() {
        if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) {
            $referer = $_SERVER['HTTP_REFERER'];
            if (strpos($referer, \Config\System::$site['host']) !== false) {
                $this->referer = $referer;
            }
        }
        $this->assign('referer', $this->referer);
    }

    /**
     * 析构函数.
     */
    public function __destruct() {
        unset($this->data);
        unset($this->param);
    }

}
